package com.atguigu.file;
import org.junit.Test;

import java.io.*;

public class BufferedTest {
    @Test
    //使用BufferedReader和BufferedWriter实现文本文件的复制
    public void test(){
        BufferedReader br= null;
        BufferedWriter bw= null;
        try {
            //创建文件和相应的流
            br = new BufferedReader(new FileReader(new File("hello.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello1.txt")));
            //读写操作
            //方式一:使用char[]数组
            //char[] cbuf=new char[1024];
           // int len;
           // while((len=br.read(cbuf))!=-1){
             //   bw.write(cbuf,0,len);
              //  bw.flush();
          //  }
            //方式二:使用String
            String data;
            while((data=br.readLine())!=null){
                bw.write(data);//data中不包含换行符
                bw.newLine();//相当于换行符
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            try {
                br.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            try {
                bw.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
